/*
  This file is part of jTopaz.
  http://gcl.cis.udel.edu/projects/jtopaz
  Copyright (C) 2009 University of Delaware

  jTopaz is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  jTopaz is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of                                                        
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with jTopaz.  If not, see <http://www.gnu.org/licenses/>.
*/

package jTopaz;

import java.util.Timer;
import java.io.File;
import java.util.TimerTask;
import org.globus.ftp.GridFTPClient;
import org.globus.ftp.FTPClient;
import org.ietf.jgss.GSSCredential;

/**
   Handles monitoring the size/updating the progress of an upload.
*/
public class RemoteFileMonitor {

    private String fileName;
    private long size;
    private Timer timer;
    private RemoteMonitorTask task;
    private GridFTPClient client;
    private FTPClient AnonClient;
	
    /**
       @param fileName the remote file to monitor
       @client the client to use for polling.
       @cred a GSSCredential, (get rid of this)
    */
    public RemoteFileMonitor(String fileName, long size, GridFTPClient client, GSSCredential cred) {
    	try {
    	
	    this.client = client;
	    this.fileName = fileName;
	    this.size = size;
	    this.task = new RemoteMonitorTask(fileName, size, client, cred);
	    timer = new Timer(true);
	    System.out.println(client.getSize(fileName));
    	}
    	catch (Exception e) {
	    System.out.println(e);
    	}
    }
    public RemoteFileMonitor(String fileName, long size, FTPClient AnonClient, GSSCredential cred) {
    	try {
    	
	    this.AnonClient = AnonClient;
	    this.fileName = fileName;
	    this.size = size;
	    this.task = new RemoteMonitorTask(fileName, size, AnonClient, cred);
	    timer = new Timer(true);
	    System.out.println(AnonClient.getSize(fileName));
    	}
    	catch (Exception e) {
	    System.out.println(e);
    	}
    }

    /**
       What is actually executed.  Creates a RemoteTask object, and schedules it for every second.
    */
    public void run() {
	long delay = 0;
	long period = 1000;
	timer.schedule(task, delay, period);
    }

    /**
       cancels the task
    */
    public void stop() {
	task.cancel();
    }
    
    /**
       Gets the current speed of the transfer
    */
    public double getSpeed() {
    	return task.getSpeed();	
    }
    
    /**
       Gets the current progress of the transfer.
    */
    public double getProgress() {
    	return task.getProgress();
    }
}

class RemoteMonitorTask extends TimerTask{

    private String fileName;
    private double size;
    private double previous;
    private GridFTPClient client;
    private FTPClient AnonClient;
    private double speed;
    private double progress;
	
    public RemoteMonitorTask(String fileName, long size, GridFTPClient client, GSSCredential cred) {
	try {
	    this.fileName = fileName;
	    this.size = new Long(size).doubleValue();
	    previous = 0;
	    this.client = client;
	    //client = new GridFTPClient("rocks-97.sdsc.edu", 2811);
	    //System.out.println("new cred created");
	    //client.authenticate(cred);
	
	    speed = 0;
	    progress = 0;
	}
	catch(Exception e) {
	    System.out.println(e);
	    e.printStackTrace();
	}
    }
    public RemoteMonitorTask(String fileName, long size, FTPClient AnonClient, GSSCredential cred) {
	try {
	    this.fileName = fileName;
	    this.size = new Long(size).doubleValue();
	    previous = 0;
	    this.AnonClient = AnonClient;
	    //client = new GridFTPClient("rocks-97.sdsc.edu", 2811);
	    //System.out.println("new cred created");
	    //client.authenticate(cred);
	
	    speed = 0;
	    progress = 0;
	}
	catch(Exception e) {
	    System.out.println(e);
	    e.printStackTrace();
	}
    }

    public void run() {

	try {
	  
	    //System.out.println("in the monitor???");
	    //System.out.println("do nothing?");
	    //System.out.println(client);
	    //if (true)
	    //	return;
	    if(client!=null){
		client.setPassive();
		client.setLocalActive();
	   	
		if (!client.exists(fileName)) {
		    System.out.println("file hasn't been created yet");
		    return;
		}
	   	
		else {
		    System.out.println("do something");
		    
		    
		    
		    double currentSize = new Long(client.size(fileName)).doubleValue();
		    System.out.println(currentSize);
		    
		    
		    double change = currentSize - previous;
		    speed = change/1024;
		    System.out.println("Speed: " + speed + " kb/s");
		    System.out.println("total size: " + size);
		    progress = currentSize/size * 100;
		    System.out.println("Progress: " + progress + "%\n");
		    
		    previous = currentSize;
		}
	    }
	    else {
		AnonClient.setPassive();
		AnonClient.setLocalActive();
	   	
		if (!AnonClient.exists(fileName)) {
		    System.out.println("file hasn't been created yet");
		    return;
		}
	   	
		else {
		    System.out.println("do something");
		    
		    
		    
		    double currentSize = new Long(AnonClient.size(fileName)).doubleValue();
		    System.out.println(currentSize);
		    
		    
		    double change = currentSize - previous;
		    speed = change/1024;
		    System.out.println("Speed: " + speed + " kb/s");
		    System.out.println("total size: " + size);
		    progress = currentSize/size * 100;
		    System.out.println("Progress: " + progress + "%\n");
		    
		    previous = currentSize;
		}
	    }
	}
	catch (Exception e) {
	    System.out.println(e);
	    e.printStackTrace();
	}
    }
    
    public double getSpeed() {
    	return speed;
    }
    
    public double getProgress() {
    	return progress;
    }
}